package com.sgzn.sgpay.service.impl;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.sgzn.sgpay.enums.ReturnResultState;
import com.sgzn.sgpay.enums.SgsResponseCode;
import com.sgzn.sgpay.exception.SgpayException;
import com.sgzn.sgpay.model.request.SgsDeviceOperLogRecordRequest;
import com.sgzn.sgpay.model.request.SgsDeviceOperateLogAddRequest;
import com.sgzn.sgpay.model.request.SgsDeviceOperateLogQueryPageRequest;
import com.sgzn.sgpay.model.response.CurrentPageInfo;
import com.sgzn.sgpay.model.response.SgsResultResponse;
import com.sgzn.sgpay.model.response.result.DeviceOperateLogQueryPageResult;
import com.sgzn.sgpay.model.response.vo.DeviceOperateLogVo;
import com.sgzn.sgpay.persistence.dao.TDeviceOperateLogMapper;
import com.sgzn.sgpay.persistence.dao.TDeviceOperateLogReadMapper;
import com.sgzn.sgpay.persistence.domain.TDevice;
import com.sgzn.sgpay.persistence.domain.TDeviceOperateLog;
import com.sgzn.sgpay.persistence.domain.TMerchant;
import com.sgzn.sgpay.persistence.manager.DeviceDaoManager;
import com.sgzn.sgpay.persistence.manager.MerchantDaoManager;
import com.sgzn.sgpay.persistence.request.TDeviceOperateLogReadRequest;
import com.sgzn.sgpay.persistence.response.TDeviceOperateLogRead;
import com.sgzn.sgpay.service.SgsDeviceOperateLogService;
import com.sgzn.sgpay.service.support.SgsResponseUtil;
import com.sgzn.sgpay.util.PageCalculator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author huangsenguang
 * @version Id: SgsDeviceOperateLogServiceImpl.java, v 0.1 2018/6/19 9:27 huangsenguang Exp $$
 */
@Service
@Slf4j
public class SgsDeviceOperateLogServiceImpl implements SgsDeviceOperateLogService {
    @Autowired
    private TDeviceOperateLogReadMapper tDeviceOperateLogReadMapper;

    @Autowired
    private TDeviceOperateLogMapper tDeviceOperateLogMapper;

    @Autowired
    private MerchantDaoManager merchantDaoManager;

    @Resource
    private DeviceDaoManager deviceDaoManager;

    @Override
    @Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public SgsResultResponse<String> add(SgsDeviceOperateLogAddRequest request) {
        //查询设备
        List<TDevice> tDevices = deviceDaoManager.getTDeviceList(request.getDeviceId());
        if (CollectionUtils.isEmpty(tDevices)) {
            return SgsResponseUtil.INSTANCE.fail(SgsResponseCode.C_DEVICE_NOT_FOUND.getResponseCode(),SgsResponseCode.C_DEVICE_NOT_FOUND.getResponseDesc());
        }
        //查询商户
        TMerchant tMerchant = merchantDaoManager.getMerchantByMerchId(request.getMchId());
        if(tMerchant == null){
            throw new SgpayException(SgsResponseCode.C_MERCHANT_NOT_FOUND.getResponseCode(),SgsResponseCode.C_MERCHANT_NOT_FOUND.getResponseDesc());
        }
        TDevice tDevice = tDevices.get(0);
        TDeviceOperateLog record = new TDeviceOperateLog();
        record.setDeviceId(request.getDeviceId());
        record.setDeviceName(tDevice.getDeviceName());
        record.setShopId(tDevice.getShopId());
        record.setOperateType(request.getOperateType());
        record.setOperateContent(request.getOperateContent());
        record.setCreatedBy(request.getMchId());
        record.setCreatedAt(new Date());
        record.setUpdatedBy(tMerchant.getMerchantName());
        record.setUpdatedAt(new Date());
        int num = this.tDeviceOperateLogMapper.insertSelective(record);
        if (num >= 1) {
            return SgsResponseUtil.INSTANCE.success(ReturnResultState.SUCCESS.getCode());
        } else {
            return SgsResponseUtil.INSTANCE.fail(SgsResponseCode.C503.getResponseCode(),SgsResponseCode.C503.getResponseDesc(), ReturnResultState.FAIL.getCode());
        }
    }

    @Override
    public SgsResultResponse<String> add(SgsDeviceOperLogRecordRequest request) {
        //查询商户
        TMerchant tMerchant = merchantDaoManager.getMerchantByMerchId(request.getMchId());
        if(tMerchant == null){
            throw new SgpayException(SgsResponseCode.C_MERCHANT_NOT_FOUND.getResponseCode(),SgsResponseCode.C_MERCHANT_NOT_FOUND.getResponseDesc());
        }
        TDeviceOperateLog record = new TDeviceOperateLog();
        record.setDeviceId(request.getDeviceId());
        record.setDeviceName(request.getDeviceName());
        record.setShopId(request.getShopId());
        record.setOperateType(request.getOperateType());
        record.setOperateContent(request.getOperateContent());
        record.setCreatedBy(request.getMchId());
        record.setCreatedAt(new Date());
        record.setUpdatedBy(tMerchant.getMerchantName());
        record.setUpdatedAt(new Date());
        int num = this.tDeviceOperateLogMapper.insertSelective(record);
        if (num >= 1) {
            return SgsResponseUtil.INSTANCE.success(ReturnResultState.SUCCESS.getCode());
        } else {
            return SgsResponseUtil.INSTANCE.fail(SgsResponseCode.C503.getResponseCode(),SgsResponseCode.C503.getResponseDesc(), ReturnResultState.FAIL.getCode());
        }
    }

    @Override
    public SgsResultResponse<DeviceOperateLogQueryPageResult> queryPage(SgsDeviceOperateLogQueryPageRequest request) {
        TDeviceOperateLogReadRequest target = new TDeviceOperateLogReadRequest();
        target.setDeviceId(request.getDeviceId());
        target.setMerchantId(request.getMchId());
        //获取总数
        int totalCount = this.tDeviceOperateLogReadMapper.count(target);
        CurrentPageInfo pageInfo = PageCalculator.getPageInfo(totalCount, request.getPageQueryParam().getPageSize(),
                request.getPageQueryParam().getCurrentPage());
        target.setStartIndex(pageInfo.getStartIndex());
        target.setEndIndex(pageInfo.getEndIndex());
        List<TDeviceOperateLogRead> tDeviceOperateLogReads = this.tDeviceOperateLogReadMapper.queryPage(target);
        return SgsResponseUtil.INSTANCE.success(assembleResponse(tDeviceOperateLogReads,pageInfo));
    }

    /**
     * 组装响应
     * @param tDeviceOperateLogReads
     * @return
     */
    private DeviceOperateLogQueryPageResult assembleResponse(List<TDeviceOperateLogRead> tDeviceOperateLogReads,CurrentPageInfo pageInfo) {
        List<DeviceOperateLogVo> deviceOperateLogVoList = new ArrayList<>(tDeviceOperateLogReads.size());
        for (TDeviceOperateLogRead tDeviceOperateLogRead : tDeviceOperateLogReads) {
            DeviceOperateLogVo vo = new DeviceOperateLogVo();
            vo.setId(tDeviceOperateLogRead.getId());
            vo.setDeviceId(tDeviceOperateLogRead.getDeviceId());
            vo.setShopId(tDeviceOperateLogRead.getShopId());
            vo.setShopName(tDeviceOperateLogRead.getShopName());
            vo.setOperateType(tDeviceOperateLogRead.getOperateType());
            vo.setOperateContent(tDeviceOperateLogRead.getOperateContent());
            vo.setCreatedBy(tDeviceOperateLogRead.getCreatedBy());
            vo.setCreatedAt(tDeviceOperateLogRead.getCreatedAt());
            vo.setUpdatedBy(tDeviceOperateLogRead.getUpdatedBy());
            vo.setUpdatedAt(tDeviceOperateLogRead.getUpdatedAt());
            vo.setDeviceName(tDeviceOperateLogRead.getDeviceName());
            deviceOperateLogVoList.add(vo);
        }
        DeviceOperateLogQueryPageResult result = new DeviceOperateLogQueryPageResult();
        result.setDeviceOperateLogVoList(deviceOperateLogVoList);
        //组装分页信息
        result.setCurrentPage(pageInfo.getCurrentPage());
        result.setPageSize(pageInfo.getPageSize());
        result.setTotalCount(pageInfo.getTotalCount());
        result.setTotalPage(pageInfo.getTotalPage());
        return result;
    }
}

